package demo;

import dao.ClassDao;
import dao.StudentDao;
import model.Classes;
import model.Student;

import java.text.MessageFormat;
import java.util.List;
import java.util.Scanner;

/**
 * @author tongchen
 * @create 2022-12-29 10:44
 */
public class exer3 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        while(true){
            System.out.println("请输入班级名称");
            String className;
            while (true) {
                className = scan.next();
                if (className == null || className.length() == 0) {
                    System.out.println("班级名称无效");
                } else {
                    break;
                }

            }
            ClassDao classDao = new ClassDao();
            Classes classIdByName = classDao.getClassIdByName(className);
            if(classIdByName==null){
                System.out.println("不存在该班级");

            }
            else {
                System.out.println("班级id为"+classIdByName.getId());
                break;
            }
        }
        System.out.println("请输入班级id");
        int classId=scan.nextInt();
        StudentDao studentDao = new StudentDao();
        List<Student> students = studentDao.selectAll(classId);
        if(students==null||students.isEmpty()){
            System.out.println("该班没有学生");
            return;
        }
        for (Student student:students) {
            String format = MessageFormat.format("学生ID:{0}\t|sn:{1}\t|name:{2}\t|email:{3}\t|classid:{4}\t|",
                    student.getId(), student.getSn(), student.getName(), student.getEmail(), student.getClassId());
            System.out.println(format);
        }

    }
}
